package my.project.controller;

import my.project.entity.UserEntity;
import my.project.service.UserService;
import my.project.util.Log;
import my.spring.annotation.Autowired;
import my.spring.annotation.Controller;
import my.spring.annotation.RequestMapping;
import my.spring.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户接口
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public Object userLogin(
            @RequestParam("username") String username,
            @RequestParam("password") String password
    ) {
        Log.log("登陆接口 username={} password={}", username, password);
        UserEntity userEntity = userService.login(username, password);
        Map<String, Object> map = new HashMap<>();
        if (userEntity != null) {
            map.put("code", 0);
            map.put("msg", "成功");
            map.put("data", userEntity);
        } else {
            map.put("code", 1);
            map.put("msg", "失败");
        }
        return map;
    }

    @RequestMapping("/")
    public Object allUser() {
        Log.log("所有用户接口");
        List<UserEntity> list = userService.findAll();
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "成功");
        map.put("data", list);
        return map;
    }
}
